﻿using LittleBee.Cells;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LittleBee.AppServ
{
	class Program
	{
		internal static bool ProgramExit { get; set; } = false;
		internal static ManualResetEvent evtAppServInitialized = new ManualResetEvent(false);

		internal static void WaitForAppServInitialized()
        {
			evtAppServInitialized.WaitOne();
        }

		static void Main(string[] args)
		{
			try
			{
				evtAppServInitialized.Reset();

				//CommMgrAppServ
				JsonRpcInterface.AppServJsonRpcObject.Start();

				CellService.Instance.Initialize();

				while (!ProgramExit)
				{
					evtAppServInitialized.Set();
					if (File.Exists("appserv.exit") == true)
					{
						File.Delete("appserv.exit");
						ProgramExit = true;
					}

					Thread.Sleep(1000);
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine($"exception occurrd: {ex.Message} : {ex.StackTrace}");
			}
			finally
			{
				JsonRpcInterface.AppServJsonRpcObject.Stop();
			}
		}
	}
}
